<template>
  <div>
    <nav-bar :title="title"></nav-bar>
    <div class="apply backgroundfff" v-if="isShowScan">
      <svg-icon class="qcCode" icon-class="qrCode" @click.native="sweep" />
    </div>
    <div id="code" style="width:100%;height:80%; background-color: #fff; text-align: center;position: absolute;left: 0;top: 5.375rem" >
    </div>
    <p class="center">扫描巡查点二维码，进行点位签到</p>
  </div>
</template>

<script>
  import signManage from '../../../../api/workAPI/signManage.js'
  let Base64 = require('js-base64').Base64
  export default {
    data() {
      return {
        isShowScan: true,
        scan: '',
        title: '点位签到'
      }
    },
    mounted() {
      const self = this
      console.log(localStorage.getItem('signStatus'))
      if (localStorage.getItem('signStatus') === '1') {
        self.title = '点位签到'
      } else {
        self.title = '加班签到'
      }
      if (window.plus) {
        self.plusReady()
      } else {
        document.addEventListener('plusready', self.plusReady, false);
      }
      document.addEventListener('DOMContentLoaded', function() {
        alert('DOMLoaded')
        self.domready = true;
        self.plusReady()
      }, false)
    },
    destroyed() {
      this.closeScan()
    },
    methods: {
      // 关闭二维码
      closeScan() {
        if (!window.plus) return;
        this.scan.close();
      },
      plusReady() {
        const self = this
        // 获取窗口
        self.scan = new plus.barcode.Barcode('code');
        self.scan.onmarked = self.onmarked;
        self.scan.start();
      },

      sweep() {
        this.isShowScan = false
        const self = this
        self.scan.start();
      },
      openDialog(text) {
        this.$dialog.alert({
          title: '扫描结果',
          message: text,
          className: 'dialog'
        }).then(() => {
          this.sweep()
        }).catch(() => {
          this.sweep()
        });
      },
      onmarked(type, result) {
        if (result.indexOf(':') !== -1) {
          let signMessage = JSON.parse(Base64.decode(result.slice(result.lastIndexOf(':') + 1)))
          if (signMessage.id) {
            if (localStorage.getItem('signStatus') === '1') {
              let data = {
                checkType: 1,
                checkPointId: signMessage.id
              }
              let projectId = localStorage.getItem('projectId')
              signManage.addSign(projectId, data).then(res => {
                this.closeScan()
                this.$router.push({
                  name: 'SignManage'
                })
                this.$toast.success('点位签到成功')
              }).catch(err => {
                console.log(err)
                this.closeScan()
                this.$router.push({
                  name: 'SignManage'
                })
              })
            } else {
              signManage.pointDetails(signMessage.id).then(res => {
                console.log(res)
                this.closeScan()
                localStorage.setItem('pointDetails', JSON.stringify(res))
                this.$router.push({
                  name: 'AddSignManage',
                })
              })
            }
          } else {
            this.openDialog('请扫描正确的二维码')
          }
        } else {
          this.openDialog('扫描错误,请重新二维码')
        }
      }
    }
  }
</script>

<style scoped lang="scss">
  .apply{
    padding: 3.125rem;
    margin-top: 6.25rem;
    text-align: center;
    .qcCode{
      font-size: 3.125rem;
    }
  }
  /deep/ .van-dialog {
    position: fixed;
    top: 60%
  }
</style>
